package io.chenglicun.modules.clc.controller.dataset;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.ExcelUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.common.validator.ValidatorUtils;
import io.chenglicun.common.validator.group.AddGroup;
import io.chenglicun.common.validator.group.DefaultGroup;
import io.chenglicun.common.validator.group.UpdateGroup;
import io.chenglicun.modules.clc.dao.ClcExcelHouseNoDao;
import io.chenglicun.modules.clc.dto.dataset.ViewExcelMapHouseInfoDTO;
import io.chenglicun.modules.clc.entity.ClcExcelHouseNoEntity;
import io.chenglicun.modules.clc.excel.dataset.ViewExcelMapHouseInfoExcel;
import io.chenglicun.modules.clc.service.ClcExcelHouseNoService;
import io.chenglicun.modules.clc.service.dataset.ViewExcelMapHouseInfoService;
import io.chenglicun.modules.clc.service.impl.ClcExcelHouseNoServiceImpl;
import io.chenglicun.modules.devtools.utils.ImportExcel2DB;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * VIEW
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2024-03-21
 */
@RestController
@RequestMapping("clc/viewexcelmaphouseinfo")
@Api(tags = "VIEW")
public class ViewExcelMapHouseInfoController {
    @Autowired
    private ViewExcelMapHouseInfoService viewExcelMapHouseInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("clc:viewexcelmaphouseinfo:page")
    public Result<PageData<ViewExcelMapHouseInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<ViewExcelMapHouseInfoDTO> page = viewExcelMapHouseInfoService.page(params);

        return new Result<PageData<ViewExcelMapHouseInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("clc:viewexcelmaphouseinfo:info")
    public Result<ViewExcelMapHouseInfoDTO> get(@PathVariable("id") Long id) {
        ViewExcelMapHouseInfoDTO data = viewExcelMapHouseInfoService.get(id);

        return new Result<ViewExcelMapHouseInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("clc:viewexcelmaphouseinfo:save")
    public Result save(@RequestBody ViewExcelMapHouseInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        viewExcelMapHouseInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("clc:viewexcelmaphouseinfo:update")
    public Result update(@RequestBody ViewExcelMapHouseInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        viewExcelMapHouseInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("clc:viewexcelmaphouseinfo:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        viewExcelMapHouseInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ViewExcelMapHouseInfoDTO> list = viewExcelMapHouseInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "VIEW", list, ViewExcelMapHouseInfoExcel.class);
    }

    @Autowired
    ClcExcelHouseNoDao excelHouseNoDao;

    @PostMapping("import")
    @ApiOperation("导入")
    @LogOperation("导入")
    public Result<String> excelHouseNoImport(@RequestParam("file") MultipartFile file) throws Exception {
        excelHouseNoDao.delete(new QueryWrapper<ClcExcelHouseNoEntity>().lambda().like(ClcExcelHouseNoEntity::getExcelHouseNo, ""));
        new ImportExcel2DB().importExcel2DB(file, 1, 2);
        return new Result<String>().ok("导入完成");
    }

}